package com.example.filecabinet.util;

import com.example.filecabinet.constant.RecordConstant;
import com.example.filecabinet.dto.req.GeneralStoreReq;
import com.example.filecabinet.entity.Lattice_info;
import com.example.filecabinet.entity.Lattice_infoExample;
import com.example.filecabinet.service.Lattice_infoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Random;

@Component
public class QueryFreeFells {
    @Autowired
    protected Lattice_infoService lattice_infoService;

    public static QueryFreeFells queryFreeFells;

    @PostConstruct
    public void init() {
        queryFreeFells = this;
        queryFreeFells.lattice_infoService = this.lattice_infoService;
    }

    /**
     * @return
     * @Author chengconghui
     * @Description //TODO 查询空格子集合
     * @Date 10:06 2020/3/23
     * @Param
     **/
    public static List<Lattice_info> queryLattice(String deviceNumber) {
        Lattice_infoExample example = new Lattice_infoExample();
        System.out.println("查询空格子集合");
        Lattice_infoExample.Criteria criteria = example.createCriteria();
        criteria.andDeviceNumberEqualTo(deviceNumber);
        criteria.andLatticeLdleStatusEqualTo("00");
        criteria.andLatticeSwithStatusNotEqualTo("02");
        List<Lattice_info> list = queryFreeFells.lattice_infoService.selectByExample(example);
        return list;
    }

    /**
     * @return
     * @Author chengconghui
     * @Description //TODO 查询是否有空格子
     * @Date 10:52 2020/3/23
     * @Param
     **/
    public static boolean getFreeFellNumber(String deviceNumber) {
        List<Lattice_info> list = queryLattice(deviceNumber);
        boolean flag = false;
        if (list.size() != 0) {
            flag = true;
        }
        return flag;
    }

    /**
     * @return
     * @Author chengconghui
     * @Description //TODO 随机分配格子
     * @Date 15:11 2020/3/24
     * @Param
     **/
    public static Lattice_info getLatticeNumber(String deviceNumber) {
        List<Lattice_info> list = queryLattice(deviceNumber);
        Lattice_info lattice_info = new Lattice_info();
        int count = list.size();
        Random random = new Random();
        int index = random.nextInt(count);
        System.out.println(index);
        for (int i = 0; i < list.size(); i++) {
            if (i == index) {
                lattice_info = list.get(i);
            }
        }
        return lattice_info;

    }

    /**
     * @return
     * @Author wangy
     * @Description 查询是否已经有该条记录了
     * 查询业务号，设备号和是否被使用
     * @Date 10:06 2020/3/23
     * @Param
     **/
    public static List<Lattice_info> queryBusinessExist(GeneralStoreReq generalStoreReq) {
        Lattice_infoExample example = new Lattice_infoExample();
        Lattice_infoExample.Criteria criteria = example.createCriteria();
        criteria.andDeviceNumberEqualTo(generalStoreReq.getDeviceNumber());
        criteria.andLatticeLdleStatusEqualTo(RecordConstant.LATTIC_LDLE_STATUS.USING);
        criteria.andBusinessNumberEqualTo(generalStoreReq.getBusinessNumber());
        criteria.andLatticeSwithStatusNotEqualTo(RecordConstant.LATTICE_SWITH_STATUS.ERROR);
        criteria.andLatticeOperatorEqualTo(RecordConstant.LATTICE_OPERATOR.CUSTOMER);
        List<Lattice_info> list = queryFreeFells.lattice_infoService.selectByExample(example);
        System.out.println("返回列表" + list.toString());
        return list;
    }
}
